package com.accenture.portal.controller.questionnaire;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.accenture.portal.model.Article;
import com.accenture.portal.model.User;
import com.accenture.portal.model.UserArticle;
import com.accenture.portal.model.questionnaire.Option;
import com.accenture.portal.model.questionnaire.Question;
import com.accenture.portal.service.questionnaire.OptionService;

@Controller
public class OptionController {
	
	@Autowired
	OptionService optionService;
	
	/**获取选择题的选项
	 * @param questionId
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "getOptionByQuestion.action")
	public void getOptionByQuestion(
			@RequestParam("questionId") Integer questionId,
			HttpServletRequest request,HttpServletResponse response,Model model) {
		List<Option> OptionList = optionService.getOptionByQuestion(questionId);
		request.getSession().setAttribute("questionId", questionId);
		model.addAttribute("questionList", OptionList);
		
		JSONArray json = JSONArray.fromObject(OptionList);
		String result = json.toString();
		try {
			response.getWriter().write(result);
			response.getWriter().flush();//清空缓冲区，完成写入
		} catch (IOException e) {
			e.printStackTrace();
		}
//		return "questionnaire/questionnaire";
	}
	
	
	
	/**添加一个选择题的选项
	 * @param option
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "addOption.action", method = RequestMethod.POST)
	public String addOption(@ModelAttribute("option") Option option,
			HttpServletRequest request, HttpServletResponse response,
			Model model) throws UnsupportedEncodingException {
		Integer questionnaireId = (Integer)request.getSession().getAttribute("questionnaireId");
		Integer questionId = (Integer)request.getSession().getAttribute("questionId");
		option.setOptionQuestion(questionId);
		optionService.addOption(option);
		return "redirect:getQuestionByQuestionnaire.action?questionnaireId=" + questionnaireId;
	}
	
	/**跳转到添加选项的编辑页面
	 * @param questionId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toAddOption.action", method = RequestMethod.GET)
	public String toAddOption(@RequestParam("questionId") Integer questionId,
			HttpServletRequest request,HttpServletResponse response, Model model) {
		request.getSession().setAttribute("questionId", questionId);
		return "questionnaire/addOption";
	}
	
	/**删除单个选项
	 * @param optionId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "deleteOption.action", method = RequestMethod.GET)
	public String deleteOption(@RequestParam("optionId") Integer optionId,
			HttpServletRequest request,HttpServletResponse response, Model model) {
		optionService.deleteOption(optionId);
		Integer questionnaireId = (Integer)request.getSession().getAttribute("questionnaireId");
		return "redirect:getQuestionByQuestionnaire.action?questionnaireId=" + questionnaireId;
	}
	
	/**删除多个选项
	 * @param checkedId
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value = "deleteOptionMulti.action")
	public void deleteOptionMulti(@RequestParam("checkedId") String checkedId,
	HttpServletRequest request, HttpServletResponse response, Model model) {
//		Integer user_id=((User)request.getSession().getAttribute("user")).getUser_id();
		String optionMulti[] = checkedId.split(",");//分割得到所有选项的id
//		articleService.markAsReadMulti(user_id, markMulti);
		for (Integer i = 0; i < optionMulti.length; i++) {
			Integer optionId = Integer.valueOf(optionMulti[i]);
			optionService.deleteOption(optionId);
		}
	}
	
	/**编辑选项
	 * @param option
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "editOption.action", method = RequestMethod.POST)
	public String editOption(@ModelAttribute Option option,
			HttpServletRequest request, HttpServletResponse response,
			Model model) throws UnsupportedEncodingException {
		option.setOptionId((Integer)request.getSession().getAttribute("optionId"));
		option.setOptionQuestion((Integer)request.getSession().getAttribute("optionQuestion"));
		optionService.editOption(option);
		Integer questionnaireId = (Integer)request.getSession().getAttribute("questionnaireId");
		return "redirect:getQuestionByQuestionnaire.action?questionnaireId=" + questionnaireId;
	}
	
	/*@RequestMapping(value = "toEditOption.action", method = RequestMethod.GET)
	public String toEditOption(@RequestParam("optionId") Integer optionId,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Option option = optionService.getOptionById(optionId);
		Integer optionQuestion=option.getOptionQuestion();
		request.getSession().setAttribute("optionId", optionId);
		request.getSession().setAttribute("optionQuestion", optionQuestion);
		model.addAttribute("optionLast", option);
		return "questionnaire/editOption";
	}*/
	
	/**跳转编辑选项页面
	 * @param optionId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toEditOption.action", method = RequestMethod.GET)
	public String toEditOption(@RequestParam("optionId") Integer optionId,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Option option = optionService.getOptionById(optionId);
		Integer optionQuestion=option.getOptionQuestion();
		request.getSession().setAttribute("optionId", optionId);
		request.getSession().setAttribute("optionQuestion", optionQuestion);
		model.addAttribute("optionLast", option);
		return "questionnaire/editOption";
	}

}
